package com.spring.boot.config.security.handler;


import com.fasterxml.jackson.databind.ObjectMapper;
import com.spring.boot.config.websocket.server.WebSocketServer;
import com.spring.boot.response.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Component("myAuthenticSucceedHandler")
public class MyAuthenticSucceedHandler implements AuthenticationSuccessHandler {
    private final static Logger log = LoggerFactory.getLogger(MyAuthenticSucceedHandler.class);
    @Override
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication)
            throws IOException, ServletException {
        Authentication authentication1 = SecurityContextHolder.getContext().getAuthentication();
        log.info("authentication1=>"+authentication1.toString());
        response.getWriter().write(new ObjectMapper().writeValueAsString(Result.OK("sign in success!",authentication1.getPrincipal())));
    }
}
